package com.qi.spzx.manager.service.impl;

import com.qi.spzx.manager.mapper.SysRoleMenuMapper;
import com.qi.spzx.manager.service.SysMenuService;
import com.qi.spzx.manager.service.SysRoleMenuService;
import com.qi.spzx.model.dto.system.AssginMenuDto;
import com.qi.spzx.model.entity.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2024/2/22 16:46
 * @Version 1.0.0
 */
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 根据角色id查询拥有的菜单
     * @param roleId
     * @return
     */
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {

        // 查询所有的菜单数据
        List<SysMenu> sysMenuList = sysMenuService.findNodes() ;

        // 查询当前角色的菜单数据
        List<Long> roleMenuIds = sysRoleMenuMapper.findSysRoleMenuByRoleId(roleId) ;

        // 将数据存储到Map中进行返回
        Map<String , Object> result = new HashMap<>() ;
        result.put("sysMenuList" , sysMenuList) ;
        result.put("roleMenuIds" , roleMenuIds) ;

        // 返回
        return result;
    }

    /**
     * 修改用户角色所具有的菜单
     * @param assginMenuDto
     */
    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        //首先获取到角色id
        Long roleId = assginMenuDto.getRoleId();
        sysRoleMenuMapper.deleteByRoleId(roleId);
        //进行菜单的添加
        List<Map<String, Number>> menuIdList = assginMenuDto.getMenuIdList();
        if (menuIdList != null && menuIdList.size() > 0) {
            sysRoleMenuMapper.doAssign(assginMenuDto) ;
        }
    }
}
